package com.example.dance.controller;

import com.example.dance.pojo.Result;
import com.example.dance.pojo.VideoPoint;
import com.example.dance.service.UploadService;
import com.example.dance.util.ReturnResultUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.List;

@RestController
public class UploadController {
    @Autowired
    private UploadService uploadService;


    @PostMapping("/upload")
    public Result upload(
            @RequestParam("video") MultipartFile videoFile,      // 接收文件
            @RequestParam("title") String title,                // 接收普通表单字段
            @RequestParam("desc") String desc,
            @RequestParam("type") int type,
            @RequestParam("nodes") String nodesJson) {          // 接收JSON字符串

        try {
            // 解析JSON字符串为对象（需引入JSON工具，如Jackson）
            ObjectMapper objectMapper = new ObjectMapper();
            List<VideoPoint> nodes;
            try{
                nodes = objectMapper.readValue(nodesJson, new TypeReference<List<VideoPoint>>() {});
            }catch (JsonProcessingException e) {
                System.out.println(e);
                return ReturnResultUtil.failure("JSON解析失败");
            }
            //System.out.println(title +","+ desc +","+ type +","+ nodes);
            type+=1;
            uploadService.uploadService(videoFile,title,desc,type,nodes);
            // 调用Service层处理数据
//            yourService.processUpload(videoFile, title, desc, type, nodes);
            return ReturnResultUtil.success();
        }  catch (IOException e) {
            System.out.println(e);
            return ReturnResultUtil.failure("文件处理异常");
        }
    }
}
